﻿using Avalonia.Controls;
using Common.Extension;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using DBCHM.Models;
using DBCHM.Service;
using DBCHM.Utils.Events;
using DBCHM.Views;
using Irihi.Avalonia.Shared.Contracts;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using Ursa.Controls;

namespace DBCHM.ViewModels;

/// <summary>
/// 连接列表界面
/// </summary>
public partial class ConnectListPageViewModel : ViewModelBase, IDialogContext
{
    private readonly IStoreService _storeService;
    private readonly IMessageService _messageService;
    private readonly IBusinessService _businessService;

    public ConnectListPageViewModel(IStoreService storeService, IMessageService messageService, IBusinessService businessService)
    {
        _storeService = storeService;
        _messageService = messageService;
        _businessService = businessService;
        LoadData().GetAwaiter().GetResult();
    }

    public ObservableCollection<DbConnectConfigDto> GridList { get; set; }

    /// <summary>
    /// 选中的数据库连接
    /// </summary>
    [ObservableProperty]
    private DbConnectConfigDto? _selectDb;

    public void Close()
    {
        RequestClose?.Invoke(this, null);
    }

    /// <summary>
    /// 页面关闭事件
    /// </summary>
    public event EventHandler<object?>? RequestClose;

    /// <summary>
    /// 新建连接
    /// </summary>
    [RelayCommand]
    private async Task AddConnect()
    {
        var options = new DialogOptions
                      {
                          Title = "连接数据库",
                          Mode = DialogMode.None,
                          Button = DialogButton.None,
                          ShowInTaskBar = false,
                          IsCloseButtonVisible = false,
                          StartupLocation = WindowStartupLocation.CenterScreen,
                          CanDragMove = true,
                          CanResize = false,
                      };
        var model = App.Current.Services.GetRequiredService<AddConnectWindowViewModel>();
        await Dialog.ShowModal<AddConnectPageView, AddConnectWindowViewModel>(model, options: options);
    }

    /// <summary>
    /// 刷新
    /// </summary>
    [RelayCommand]
    private void ReloadData()
    {
        //LoadData();
    }

    /// <summary>
    /// 连接数据库
    /// </summary>
    [RelayCommand]
    private async Task ConnectDb()
    {
        if (SelectDb is null)
        {
            _messageService.SendMessage("请选择数据库");
            return;
        }

        var testResult = await _businessService.ConnectTestAsync(SelectDb);
        if (!testResult)
        {
            _messageService.SendMessage("连接测试失败");
            return;
        }

        DbChmConst.CurrConnectId = SelectDb.Id.ToInt32();

        RequestClose?.Invoke(this, DialogResult.OK);
    }

    /// <summary>
    /// 加载数据
    /// </summary>
    private async Task LoadData()
    {
        var list = await _storeService.GetConfigListAsync();
        GridList = new ObservableCollection<DbConnectConfigDto>(list);
    }
}